﻿<cfscript>
/** 
* @hint "courseArrange - Arrangement TimeTable"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.timeTableListener" 
	output=false 
	depends="timeTablePropertyAdvice" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "获取考试可用时间分布表"
	* 
	* @event "MachII Event Object"
	*/
	public void function getExamTimeTable( required MachII.framework.Event event ) output=false {

		var timeTable = arrayNew(2);

		var weekDay = lecture = 0;
		
		var propAdvice = gettimeTablePropertyAdvice().init();
		
		var sql = "SELECT wdidx, lecidx, props FROM t_timetable "; 
		
		var queryObj = new query( datasource=application.dnsSlave );
		var rs_timeSet = queryObj.execute( sql=sql ).getResult();
	
		var target = event.getArg("Target", "FinalExam");
		
		/* 如果没有设置 则采用默认值初始化 */
		if (rs_timeSet.recordCount eq 0) {
			
			/* Every SQL operation is TRANSACATION*/
			transaction {
			
				for(weekDay=1; weekDay lte 7; weekDay++){
					for(lecture=1; lecture lte 5; lecture++){
						
						sql = "INSERT INTO t_timetable(
									wdidx, 
									lecidx, 
									props 
								)VALUES{
									:weekDay, 
									:lectureIndex, 
									:usageProps 
								}"; 
						
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="weekDay", value=weekDay, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="lectureIndex", value=lecture, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="usageProps", value=propAdvice.stringProperty(), cfsqltype="cf_sql_varchar" );
						queryObj.execute( sql=sql );
												
						timeTable[weekDay][lecture] = 1;
					
					}
				}
			
			}
			
		}
	
		for ( var i=1; i LTE rs_timeSet.recordCount; i++){
			
			propAdvice.parseProperty(rs_timeSet["props"][i]);
			
			switch ( target ) {
				
				case "FinalExam":
					if( propAdvice.getFinalExam() ){
						timeTable[rs_timeSet["wdidx"][i]][rs_timeSet["lecidx"][i]] = 1;
					}
					else {
						timeTable[rs_timeSet["wdidx"][i]][rs_timeSet["lecidx"][i]] = 0;
					}
					break;
				
				case "HalfExam":
					if( propAdvice.getHalfExam() ){
						timeTable[rs_timeSet["wdidx"][i]][rs_timeSet["lecidx"][i]] = 1;
					}
					else {
						timeTable[rs_timeSet["wdidx"][i]][rs_timeSet["lecidx"][i]] = 0;
					}
					break;
				
				case "DisperseExam":
					if( propAdvice.getDisperseExam() ){
						timeTable[rs_timeSet["wdidx"][i]][rs_timeSet["lecidx"][i]] = 1;
					}
					else {
						timeTable[rs_timeSet["wdidx"][i]][rs_timeSet["lecidx"][i]] = 0;
					}
					break;
				
				case "ResitExam":
					if( propAdvice.getResitExam() ){
						timeTable[rs_timeSet["wdidx"][i]][rs_timeSet["lecidx"][i]] = 1;
					}
					else {
						timeTable[rs_timeSet["wdidx"][i]][rs_timeSet["lecidx"][i]] = 0;
					}
					break;
			}
			
			
		}
		
		event.setArg("timeTable", timeTable);
		
	}


	/**
	* @hint "获取单开班可用时间分布表"
	* 
	* @event "MachII Event Object"
	*/
	public void function getSportTaskTimeTable( required MachII.framework.Event event ) output=false {
		
		var timeTable = arrayNew(2);
		var ts = [];
		
		var weekDay = lecture = 0;
		
		var propAdvice = gettimeTablePropertyAdvice().init();
		var sql = "select t.wdidx,t.lecidx,t.props from t_timetable t";
		var queryObj = new Query(datasource = application.dnsSlave);
		var rs_timeSet = queryObj.execute(sql = sql).getResult();
		
		var timeSet = "";
		
		/* 如果没有设置 则采用默认值初始化 */
		if (rs_timeSet.recordCount eq 0) {
			
			/* Every SQL operation is TRANSACATION*/
			transaction {
			
				for(weekDay=1; weekDay lte 7; weekDay++){
					for(lecture=1; lecture lte 6; lecture++){
					
						sql = "	INSERT INTO 
									t_timetable(
										wdidx,
										lecidx,
										props
									)VALUES(
										:weekDay ,
										:lecture ,
										:usageProps 
									)";
						
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "weekDay", value = weekDay, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "lecture", value = lecture, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "usageProps", value = propAdvice.stringProperty(), cfsqltype = "cf_sql_varchar");
						queryObj.execute(sql = sql);
						
						timeTable[weekDay][lecture] = 1;
					
					}
				}
			
			}
			
		}
		
		for (var a = 1; a lte rs_timeSet.recordCount; a++) {
			
			propAdvice.parseProperty(rs_timeSet["props"][a]);
			
			if( propAdvice.getSportTask() ){
				timeTable[rs_timeSet["wdidx"][a]][rs_timeSet["lecidx"][a]] = 1;
			}
			else {
				timeTable[rs_timeSet["wdidx"][a]][rs_timeSet["lecidx"][a]] = 0;
			}
		}
		
		event.setArg("timeTable", timeTable);
		
	}


	/**
	* @hint "获取单开班可用时间分布表"
	* 
	* @event "MachII Event Object"
	*/
	public void function getOpenTaskTimeTable( required MachII.framework.Event event ) output=false {
		
		var timeTable = arrayNew(2);
		var ts = [];
		
		var weekDay = lecture = 0;
		
		var propAdvice = gettimeTablePropertyAdvice().init();
		
		
		var sql = "select t.wdidx,t.lecidx,t.props from t_timetable t";
		var queryObj = new Query(datasource = application.dnsSlave);
		var rs_timeSet = queryObj.execute(sql = sql).getResult();
		
		var timeSet = "";
		
		/* 如果没有设置 则采用默认值初始化 */
		if (rs_timeSet.recordCount eq 0) {
			
			/* Every SQL operation is TRANSACATION*/
			transaction {
			
				for(weekDay=1; weekDay lte 7; weekDay++){
					for(lecture=1; lecture lte 6; lecture++){
						sql = "	INSERT INTO 
									t_timetable(
										wdidx,
										lecidx,
										props
									)VALUES(
										:weekDay ,
										:lecture ,
										:usageProps 
									)";
						
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "weekDay", value = weekDay, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "lecture", value = lecture, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "usageProps", value = propAdvice.stringProperty(), cfsqltype = "cf_sql_varchar");
						queryObj.execute(sql = sql);
						
						
						timeTable[weekDay][lecture] = 1;
					
					}
				}
			
			}
			
		}
		for (var a = 1; a lte rs_timeSet.recordCount; a++) {
			

			propAdvice.parseProperty(rs_timeSet["props"][a]);
			
			if( propAdvice.getOpenTask() ){
				timeTable[rs_timeSet["wdidx"][a]][rs_timeSet["lecidx"][a]] = 1;
			}
			else {
				timeTable[rs_timeSet["wdidx"][a]][rs_timeSet["lecidx"][a]] = 0;
			}
		}
		
		event.setArg("timeTable", timeTable);
		
	}

	
	/**
	* @hint "获取全校通选课可用时间分布表"
	* 
	* @event "MachII Event Object"
	*/
	public void function getCommonTaskTimeTable( required MachII.framework.Event event ) output=false {

		var timeTable = arrayNew(2);
		var ts = [];
		
		var weekDay = lecture = 0;
		
		var propAdvice = gettimeTablePropertyAdvice().init();
		var sql = "select t.wdidx,t.lecidx,t.props from t_timetable t";
		var queryObj = new Query(datasource = application.dnsSlave);
		var rs_timeSet = queryObj.execute(sql = sql).getResult();
	//	var timeSet = "";
		
		/* 如果没有设置 则采用默认值初始化 */
		if (rs_timeSet.recordCount eq 0) {
			
			/* Every SQL operation is TRANSACATION*/
			transaction {
			
				for(weekDay=1; weekDay lte 7; weekDay++){
					for(lecture=1; lecture lte 6; lecture++){
					
						sql = "	INSERT INTO 
									t_timetable(
										wdidx,
										lecidx,
										props
									)VALUES(
										:weekDay ,
										:lecture ,
										:usageProps 
									)";
						
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "weekDay", value = weekDay, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "lecture", value = lecture, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "usageProps", value = propAdvice.stringProperty(), cfsqltype = "cf_sql_varchar");
						queryObj.execute(sql = sql);
						
						timeTable[weekDay][lecture] = 1;
					
					}
				}
			
			}
			
		}
		
		for (var a = 1; a lte rs_timeSet.recordCount; a++) {
			
			propAdvice.parseProperty(rs_timeSet["props"][a]);
			
			if( propAdvice.getCommonTask() ){
				timeTable[rs_timeSet["wdidx"][a]][rs_timeSet["lecidx"][a]] = 1;
			}
			else {
				timeTable[rs_timeSet["wdidx"][a]][rs_timeSet["lecidx"][a]] = 0;
			}
		}
		
		event.setArg("timeTable", timeTable);
		
	}
	
	/**
	* @hint "获取计划课程可用时间分布表"
	* 
	* @event "MachII Event Object"
	*/
	public void function getProgramTaskTimeTable( required MachII.framework.Event event ) output=false {

		var timeTable = arrayNew(2);
		var ts = [];
		
		var weekDay = lecture = 0;
		
		var propAdvice = gettimeTablePropertyAdvice().init();
		var sql = "select t.wdidx,t.lecidx,t.props from t_timetable t";
		var queryObj = new Query(datasource = application.dnsSlave);
		var rs_timeSet = queryObj.execute(sql = sql).getResult();
		
		var timeSet = "";
		
		/* 如果没有设置 则采用默认值初始化 */
		if (rs_timeSet.recordCount eq 0) {
			
			/* Every SQL operation is TRANSACATION*/
			transaction {
			
				for(weekDay=1; weekDay lte 7; weekDay++){
					for(lecture=1; lecture lte 6; lecture++){
						sql = "	INSERT INTO 
									t_timetable(
										wdidx,
										lecidx,
										props
									)VALUES(
										:weekDay ,
										:lecture ,
										:usageProps 
									)";
						
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "weekDay", value = weekDay, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "lecture", value = lecture, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "usageProps", value = propAdvice.stringProperty(), cfsqltype = "cf_sql_varchar");
						queryObj.execute(sql = sql);
						
						timeTable[weekDay][lecture] = 1;
					
					}
				}
			
			}
			
		}
		
		for(a = 1; a lte rs_timeSet.recordCount; a++){

			propAdvice.parseProperty(rs_timeSet['props'][a]);

			if( propAdvice.getProgramTask() ){
				timeTable[rs_timeSet['wdidx'][a]][rs_timeSet['lecidx'][a]] = 1;
			} else {
				timeTable[rs_timeSet['wdidx'][a]][rs_timeSet['lecidx'][a]] = 0;
			}
		}
		
		event.setArg("timeTable", timeTable);
		
	}
	
}

</cfscript>